package com.itheima.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.model.mongo.Publish;
import com.itheima.commons.model.mongo.RecommendQuanZi;
import com.itheima.dubbo.interfaces.PublishApi;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tanhua-quanzi",
        consumerGroup = "tanhua-quanzi-consumer")
public class QuanZiMsgConsumer implements RocketMQListener<String> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference
    private PublishApi publishApi;

    @Override
    public void onMessage(String msg) {
        // msg JSON格式的字符串
        try {
            JSONObject jsonObject = JSONUtil.parseObj(msg);
            // 用户的ID　谁操作的圈子
            Long userId = jsonObject.getLong("userId");
            // 时间  操作的时间
            Long date = jsonObject.getLong("date");
            // publishId
            String publishId = jsonObject.getStr("publishId");
            // 操作的类型  点赞/评论/喜欢
            Integer type = jsonObject.getInt("type");

            // 根据publishId查询对应的PID
            Publish publish = publishApi.queryPublishById(publishId);

            RecommendQuanZi recommendQuanZi = new RecommendQuanZi();
            recommendQuanZi.setId(new ObjectId());
            recommendQuanZi.setUserId(userId);
            recommendQuanZi.setPublishId(publish.getPid());
            recommendQuanZi.setScore(0.0D);
            recommendQuanZi.setDate(date);

            // 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
            // 发送一个圈子  你自己对这个发送的内容应该是感兴趣的
            // 接口设计 图片 和 文本
            switch (type) {
                case 1: {
                    double score = 0d;

                    //获取图片数
                    score += CollUtil.size(publish.getMedias());

                    //获取文本的长度
                    //文字长度：50以内1分，50~100之间2分，100以上3分
                    int length = StrUtil.length(publish.getText());
                    if (length >= 0 && length < 50) {
                        score += 1;
                    } else if (length < 100) {
                        score += 2;
                    } else {
                        score += 3;
                    }

                    recommendQuanZi.setScore(score);
                    break;
                }
                case 2: {
                    recommendQuanZi.setScore(1d);
                    break;
                }
                case 3: {
                    recommendQuanZi.setScore(5d);
                    break;
                }
                case 4: {
                    recommendQuanZi.setScore(8d);
                    break;
                }
                case 5: {
                    recommendQuanZi.setScore(10d);
                    break;
                }
                case 6: {
                    recommendQuanZi.setScore(-5d);
                    break;
                }
                case 7: {
                    recommendQuanZi.setScore(-8d);
                    break;
                }
                default: {
                    recommendQuanZi.setScore(0d);
                    break;
                }
            }

            //数据保存到MongoDB中
            this.mongoTemplate.save(recommendQuanZi);
        } catch (Exception e) {
            System.out.println("处理消息出错！msg = " + msg);
        }
    }
}
